package com.falling.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.falling.reggie.common.BaseContext;
import com.falling.reggie.common.R;
import com.falling.reggie.entity.AddressBook;
import com.falling.reggie.mapper.AddressBookMapper;
import com.falling.reggie.service.AddressBookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook>
        implements AddressBookService {



    @Override
    @Transactional
    public boolean setDefaultAddress(AddressBook addressBook) {
        // 将userId对应的所有地址默认状态都设为0（取消默认）
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        updateWrapper.set(AddressBook::getIsDefault, 0);
        super.update(updateWrapper);

        // 将addressBookId对应的地址设为默认
        addressBook.setIsDefault(1);
        return super.updateById(addressBook);
    }
}
